package com.yysoft.core.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yysoft.core.dao.def.SysMenuQueryMapper;
import com.yysoft.core.dao.def.SysUserMapper;
import com.yysoft.core.dao.opencart1.OcDownloadMapper;
import com.yysoft.core.model.OcDownload;
import com.yysoft.core.model.SysMenu;
import com.yysoft.core.model.SysUser;
import com.yysoft.core.model.SysUserExample;
import com.yysoft.core.web.model.MenuModel;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yysoft.core.service.IUserService;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Transactional("transactionManager")
public class UserService implements IUserService {

	@Autowired
	private SysUserMapper sysUserMapper;

	@Autowired
	private SysMenuQueryMapper sysMenuQueryMapper;

	@Autowired
	private OcDownloadMapper ocDownloadMapper;

	public SysUser findByUsername(String username) {
		SysUserExample example = new SysUserExample();
		SysUserExample.Criteria criteria = example.createCriteria();
		criteria.andUserNameEqualTo(username);
		List<SysUser> list = sysUserMapper.selectByExample(example);
		if(list != null && list.size() > 0){
			return list.get(0);
		}
		return null;
	}

	public List<MenuModel> getMenuList(String userId, String jsonStr, String parentId) {
		Map<String, String> params = new HashMap<>();
		params.put("jsonStr", jsonStr);
		params.put("userId", userId);
		if (StringUtils.isNotEmpty(parentId))
			params.put("parentId", parentId);
		List<SysMenu> list = sysMenuQueryMapper.getMenuList(params);
		return list.parallelStream().map(sysMenu -> {
			MenuModel menuModel = new MenuModel();
			menuModel.setIcon(sysMenu.getMenuIcon());
			menuModel.setIndex(sysMenu.getMenuKey());
			menuModel.setTitle(sysMenu.getMenuName());
			if(StringUtils.isEmpty(parentId)){
				menuModel.setSubs(this.getMenuList(userId, jsonStr, sysMenu.getMenuId()));
			}
			return menuModel;
		}).collect(Collectors.toList());
	}

	@Override
	public PageInfo<SysUser> getUserList(int curPage, int pageCount, String userName) {
		PageHelper.startPage(curPage, pageCount);
		SysUserExample example = new SysUserExample();
		SysUserExample.Criteria criteria = example.createCriteria();
		criteria.andStateEqualTo("1");
		if(StringUtils.isNotEmpty(userName)){
			criteria.andUserNameLike("%" + userName + "%");
		}
		example.setOrderByClause("create_time desc");
		List<SysUser> list = sysUserMapper.selectByExample(example);
		return new PageInfo<>(list);
	}

	@Override
	public void removeUsers(String userIds) {
		String[] userIdArr = userIds.split(",");
		for (String userId : userIdArr) {
			SysUser user = sysUserMapper.selectByPrimaryKey(userId);
			user.setState("0");
			sysUserMapper.updateByPrimaryKey(user);
		}
		OcDownload ocDownload = new OcDownload();
		ocDownload.setFilename("111");
		ocDownload.setMask("000");
		ocDownload.setDateAdded(new Date());
		ocDownloadMapper.insertSelective(ocDownload);
		int i = 1/0;
	}

}
